﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using StoreVnCore.Business;
using StoreVnCore.Entity;
using StoreVnCore.Common;

namespace StoreVnAdmin
{
    public partial class Form1 : Form
    {
        protected static int editStatus = 0; //Edit = 0, Add new = 1

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            FillTypeOfContentListBox();
            InitControl();
        }

        protected void FillTypeOfContentListBox()
        {
            DataTable dt = (new TypeOfContentBO()).SelectAll();
            ltbTypeOfContent.DisplayMember = FieldNames.TypeOfContent.Name;
            ltbTypeOfContent.ValueMember = FieldNames.TypeOfContent.Id;
            ltbTypeOfContent.DataSource = dt;

            ltbTypeOfContentCategory.DisplayMember = FieldNames.TypeOfContent.Name;
            ltbTypeOfContentCategory.ValueMember = FieldNames.TypeOfContent.Id;
            ltbTypeOfContentCategory.DataSource = dt;

            ltbTypeOfContentFormat.DisplayMember = FieldNames.TypeOfContent.Name;
            ltbTypeOfContentFormat.ValueMember = FieldNames.TypeOfContent.Id;
            ltbTypeOfContentFormat.DataSource = dt;

            ltbTypeOfContentMarket.DisplayMember = FieldNames.TypeOfContent.Name;
            ltbTypeOfContentMarket.ValueMember = FieldNames.TypeOfContent.Id;
            ltbTypeOfContentMarket.DataSource = dt;

            ltbTypeOfContentPlatform.DisplayMember = FieldNames.TypeOfContent.Name;
            ltbTypeOfContentPlatform.ValueMember = FieldNames.TypeOfContent.Id;
            ltbTypeOfContentPlatform.DataSource = dt;
        }

        protected void FillItemsListbox(string typeOfContent)
        {
            int tab = tabControl1.SelectedIndex;
            switch (tab)
            {
                case 0:
                    {
                        
                    }
                    break;
                case 1:
                    {
                        DataTable dt = (new ContentCategoryBO()).SelectByTypeOfContent(typeOfContent);
                        ltbCategory.DisplayMember = FieldNames.ContentCategory.Name;
                        ltbCategory.ValueMember = FieldNames.ContentCategory.Id;
                        ltbCategory.DataSource = dt;
                    }
                    break;
                case 2:
                    {
                        DataTable dt = (new ContentFormatBO()).SelectByTypeOfContent(typeOfContent);
                        ltbFormat.DisplayMember = FieldNames.ContentFormat.Name;
                        ltbFormat.ValueMember = FieldNames.ContentFormat.Id;
                        ltbFormat.DataSource = dt;
                    }
                    break;
                case 3:
                    {
                        DataTable dt = (new ContentMarketBO()).SelectByTypeOfContent(typeOfContent);
                        ltbMarket.DisplayMember = FieldNames.ContentMarket.Name;
                        ltbMarket.ValueMember = FieldNames.ContentMarket.Id;
                        ltbMarket.DataSource = dt;
                    }
                    break;
                case 4:
                    {
                        DataTable dt = (new ContentPlatformBO()).SelectByTypeOfContent(typeOfContent);
                        ltbPlatform.DisplayMember = FieldNames.ContentPlatform.Name;
                        ltbPlatform.ValueMember = FieldNames.ContentPlatform.Id;
                        ltbPlatform.DataSource = dt;
                    }
                    break;
                default:

                    break;
            }
        }

        protected void FillDetails(int index)
        {
            InitControl();
            int tab = tabControl1.SelectedIndex;
            switch (tab)
            {
                case 0:
                    {

                    }
                    break;
                case 1:
                    {
                        ContentCategoryEntity category = (new ContentCategoryBO()).SelectOne(index);
                        if (category != null)
                        {
                            txtCategoryId.Text = category.Id.ToString();
                            txtCategoryName.Text = category.Name;
                            txtCategoryDesc.Text = category.Description;
                        }
                        else
                        {
                            txtCategoryId.Text = "";
                            txtCategoryName.Text = "";
                            txtCategoryDesc.Text = "";
                        }
                    }
                    break;
                case 2:
                    {
                        ContentFormatEntity format = (new ContentFormatBO()).SelectOne(index);
                        if (format != null)
                        {
                            txtFormatId.Text = format.Id.ToString();
                            txtFormatName.Text = format.Name;
                            txtFormatDesc.Text = format.Description;
                        }
                        else
                        {
                            txtFormatId.Text = "";
                            txtFormatName.Text = "";
                            txtFormatDesc.Text = "";
                        }
                    }
                    break;
                case 3:
                    {
                        ContentMarketEntity market = (new ContentMarketBO()).SelectOne(index);
                        if (market != null)
                        {
                            txtMarketId.Text = market.Id.ToString();
                            txtMarketName.Text = market.Name;
                            txtMarketDesc.Text = market.Description;
                        }
                        else
                        {
                            txtMarketId.Text = "";
                            txtMarketName.Text = "";
                            txtMarketDesc.Text = "";
                        }
                    }
                    break;
                case 4:
                    {
                        ContentPlatformEntity platform = (new ContentPlatformBO()).SelectOne(index);
                        if (platform != null)
                        {
                            txtPlatformId.Text = platform.Id.ToString();
                            txtPlatformName.Text = platform.Name;
                            txtPlatformDesc.Text = platform.Description;
                        }
                        else
                        {
                            txtPlatformId.Text = "";
                            txtPlatformName.Text = "";
                            txtPlatformDesc.Text = "";
                        }
                    }
                    break;
                default:

                    break;
            }
        }

        protected void InitControl()
        {
            btnAdd.Enabled = true;
            btnEdit.Enabled = true;
            btnDelete.Enabled = true;
            btnSave.Enabled = false;

            txtTypeOfContentId.Enabled = false;
            txtTypeOfContentName.Enabled = false;
            txtTypeOfContentDesc.Enabled = false;

            txtFormatId.Enabled = false;
            txtFormatName.Enabled = false;
            txtFormatDesc.Enabled = false;

            txtCategoryId.Enabled = false;
            txtCategoryName.Enabled = false;
            txtCategoryDesc.Enabled = false;

            txtMarketId.Enabled = false;
            txtMarketName.Enabled = false;
            txtMarketDesc.Enabled = false;

            txtPlatformId.Enabled = false;
            txtPlatformName.Enabled = false;
            txtPlatformDesc.Enabled = false;

            int tab = tabControl1.SelectedIndex;
            switch (tab)
            {
                case 0:
                    {
                        if (ltbTypeOfContent.Items.Count > 0)
                        {
                            btnEdit.Enabled = true;
                        }
                        else
                        {
                            btnEdit.Enabled = false;
                        }
                    }
                    break;
                case 1:
                    {
                        if (ltbCategory.Items.Count > 0)
                        {
                            btnEdit.Enabled = true;
                        }
                        else
                        {
                            btnEdit.Enabled = false;
                        }
                    }
                    break;
                case 2:
                    {
                        if (ltbFormat.Items.Count > 0)
                        {
                            btnEdit.Enabled = true;
                        }
                        else
                        {
                            btnEdit.Enabled = false;
                        }
                    }
                    break;
                case 3:
                    {
                        if (ltbMarket.Items.Count > 0)
                        {
                            btnEdit.Enabled = true;
                        }
                        else
                        {
                            btnEdit.Enabled = false;
                        }
                    }
                    break;
                case 4:
                    {
                        if (ltbPlatform.Items.Count > 0)
                        {
                            btnEdit.Enabled = true;
                        }
                        else
                        {
                            btnEdit.Enabled = false;
                        }
                    }
                    break;
                default:

                    break;
            }
        }

        protected void EnableControlToEdit()
        {
            int tab = tabControl1.SelectedIndex;
            editStatus = 0;
            btnSave.Enabled = true;
            btnDelete.Enabled = false;
            switch (tab)
            {
                case 0:
                    {
                        txtTypeOfContentName.Enabled = true;
                        txtTypeOfContentDesc.Enabled = true;
                        txtTypeOfContentName.Focus();
                    }
                    break;
                case 1:
                    {
                        txtCategoryName.Enabled = true;
                        txtCategoryDesc.Enabled = true;
                        txtCategoryName.Focus();
                    }
                    break;
                case 2:
                    {
                        txtFormatName.Enabled = true;
                        txtFormatDesc.Enabled = true;
                        txtFormatName.Focus();
                    }
                    break;
                case 3:
                    {
                        txtMarketName.Enabled = true;
                        txtMarketDesc.Enabled = true;
                        txtMarketName.Focus();
                    }
                    break;
                case 4:
                    {
                        txtPlatformName.Enabled = true;
                        txtPlatformDesc.Enabled = true;
                        txtPlatformName.Focus();
                    }
                    break;
                default:

                    break;
            }
        }

        protected void ResetControlTextToAddNew()
        {
            int tab = tabControl1.SelectedIndex;
            editStatus = 1;
            btnEdit.Enabled = false;
            btnDelete.Enabled = false;
            btnSave.Enabled = true;
            switch (tab)
            {
                case 0:
                    {
                        
                        txtTypeOfContentId.Text = "";
                        txtTypeOfContentName.Text = "";
                        txtTypeOfContentDesc.Text = "";
                        txtTypeOfContentName.Focus();
                    }
                    break;
                case 1:
                    {

                        txtCategoryId.Text = "";
                        txtCategoryName.Text = "";
                        txtCategoryDesc.Text = "";
                        txtCategoryName.Focus();
                    }
                    break;
                case 2:
                    {

                        txtFormatId.Text = "";
                        txtFormatName.Text = "";
                        txtFormatDesc.Text = "";
                        txtFormatName.Focus();
                    }
                    break;
                case 3:
                    {

                        txtMarketId.Text = "";
                        txtMarketName.Text = "";
                        txtMarketDesc.Text = "";
                        txtMarketName.Focus();
                    }
                    break;
                case 4:
                    {
                        txtPlatformId.Text = "";
                        txtPlatformName.Text = "";
                        txtPlatformDesc.Text = "";
                        txtPlatformName.Focus();
                    }
                    break;
                default:

                    break;
            }
        }

        #region Event Handle

        private void btnAdd_Click(object sender, EventArgs e)
        {
            EnableControlToEdit();
            ResetControlTextToAddNew();
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            EnableControlToEdit();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            int tab = tabControl1.SelectedIndex;
            switch (tab)
            {
                case 0:
                    {
                        TypeOfContentEntity newObj = null;
                        if (editStatus == 0) //Update
                        {
                            TypeOfContentEntity typeOfContent = new TypeOfContentEntity();
                            typeOfContent.Id = (int)ltbTypeOfContent.SelectedValue;
                            typeOfContent.Name = txtTypeOfContentName.Text.Trim();
                            typeOfContent.Description = txtTypeOfContentDesc.Text.Trim();
                            newObj = (new TypeOfContentBO()).Save(typeOfContent);
                        }
                        else //Add new
                        {
                            TypeOfContentEntity typeOfContent = new TypeOfContentEntity();
                            typeOfContent.Name = txtTypeOfContentName.Text.Trim();
                            typeOfContent.Description = txtTypeOfContentDesc.Text.Trim();
                            newObj = (new TypeOfContentBO()).AddNew(typeOfContent);
                        }
                        FillTypeOfContentListBox();
                    }
                    break;
                case 1:
                    {
                        ContentCategoryEntity newObj = null;
                        if (editStatus == 0) //Update
                        {
                            ContentCategoryEntity category = new ContentCategoryEntity();
                            category.Id = (int)ltbCategory.SelectedValue;
                            category.TypeOfContent = ltbTypeOfContentCategory.Text;
                            category.Name = txtCategoryName.Text.Trim();
                            category.Description = txtCategoryDesc.Text.Trim();
                            newObj = (new ContentCategoryBO()).Save(category);
                        }
                        else //Add new
                        {
                            ContentCategoryEntity category = new ContentCategoryEntity();
                            category.TypeOfContent = ltbTypeOfContentCategory.Text;
                            category.Name = txtCategoryName.Text.Trim();
                            category.Description = txtCategoryDesc.Text.Trim();
                            newObj = (new ContentCategoryBO()).AddNew(category);
                        }
                        FillItemsListbox(ltbTypeOfContentCategory.Text);
                        FillDetails(Convert.ToInt16(ltbCategory.SelectedValue));
                        ltbCategory.SelectedValue = newObj.Id;
                    }
                    break;
                case 2:
                    {
                        ContentFormatEntity newObj = null;
                        if (editStatus == 0) //Update
                        {
                            ContentFormatEntity format = new ContentFormatEntity();
                            format.Id = (int)ltbFormat.SelectedValue;
                            format.TypeOfContent = ltbTypeOfContentFormat.Text;
                            format.Name = txtFormatName.Text.Trim();
                            format.Description = txtFormatDesc.Text.Trim();
                            newObj = (new ContentFormatBO()).Save(format);
                        }
                        else //Add new
                        {
                            ContentFormatEntity format = new ContentFormatEntity();
                            format.TypeOfContent = ltbTypeOfContentFormat.Text;
                            format.Name = txtFormatName.Text.Trim();
                            format.Description = txtFormatDesc.Text.Trim();
                            newObj = (new ContentFormatBO()).AddNew(format);
                        }
                        FillItemsListbox(ltbTypeOfContentFormat.Text);
                        FillDetails(Convert.ToInt16(ltbFormat.SelectedValue));
                        ltbFormat.SelectedValue = newObj.Id;
                    }
                    break;
                case 3:
                    {
                        ContentMarketEntity newObj = null;
                        if (editStatus == 0) //Update
                        {
                            ContentMarketEntity market = new ContentMarketEntity();
                            market.Id = (int)ltbMarket.SelectedValue;
                            market.TypeOfContent = ltbTypeOfContentMarket.Text;
                            market.Name = txtMarketName.Text.Trim();
                            market.Description = txtMarketDesc.Text.Trim();
                            newObj = (new ContentMarketBO()).Save(market);
                        }
                        else //Add new
                        {
                            ContentMarketEntity market = new ContentMarketEntity();
                            market.TypeOfContent = ltbTypeOfContentMarket.Text;
                            market.Name = txtMarketName.Text.Trim();
                            market.Description = txtMarketDesc.Text.Trim();
                            newObj = (new ContentMarketBO()).AddNew(market);
                        }
                        FillItemsListbox(ltbTypeOfContentMarket.Text);
                        FillDetails(Convert.ToInt16(ltbMarket.SelectedValue));
                        ltbMarket.SelectedValue = newObj.Id;
                    }
                    break;
                case 4:
                    {
                        ContentPlatformEntity newObj = null;
                        if (editStatus == 0) //Update
                        {
                            ContentPlatformEntity platform = new ContentPlatformEntity();
                            platform.Id = (int)ltbPlatform.SelectedValue;
                            platform.TypeOfContent = ltbTypeOfContentPlatform.Text;
                            platform.Name = txtPlatformName.Text.Trim();
                            platform.Description = txtPlatformDesc.Text.Trim();
                            newObj = (new ContentPlatformBO()).Save(platform);
                        }
                        else //Add new
                        {
                            ContentPlatformEntity platform = new ContentPlatformEntity();
                            platform.TypeOfContent = ltbTypeOfContentPlatform.Text;
                            platform.Name = txtPlatformName.Text.Trim();
                            platform.Description = txtPlatformDesc.Text.Trim();
                            newObj = (new ContentPlatformBO()).AddNew(platform);
                        }
                        FillItemsListbox(ltbTypeOfContentPlatform.Text);
                        FillDetails(Convert.ToInt16(ltbPlatform.SelectedValue));
                        ltbPlatform.SelectedValue = newObj.Id;
                    }
                    break;
                default:

                    break;
            }

            InitControl();
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            int tab = tabControl1.SelectedIndex;
            switch (tab)
            {
                case 0:
                    {
                        if (ltbTypeOfContent.SelectedValue != null)
                        {
                            TypeOfContentBO obj = new TypeOfContentBO();
                            obj.Delete((int)ltbTypeOfContent.SelectedValue);
                            FillTypeOfContentListBox();
                        }
                        else
                        {
                            MessageBox.Show("Chú ý!", "Bạn phải chọn một kiểu nội dung để xóa");
                        }
                    }
                    break;
                case 1:
                    {
                        if (ltbCategory.SelectedValue != null)
                        {
                            ContentCategoryBO obj = new ContentCategoryBO();
                            obj.Delete((int)ltbCategory.SelectedValue);
                            FillItemsListbox(ltbTypeOfContentCategory.Text);
                        }
                        else
                        {
                            MessageBox.Show("Bạn phải chọn một thể loại để xóa", "Chú ý!");
                        }
                    }
                    break;
                case 2:
                    {
                        if (ltbFormat.SelectedValue != null)
                        {
                            ContentFormatBO obj = new ContentFormatBO();
                            obj.Delete((int)ltbFormat.SelectedValue);
                            FillItemsListbox(ltbTypeOfContentFormat.Text);
                        }
                        else
                        {
                            MessageBox.Show("Bạn phải chọn một định dạng để xóa", "Chú ý!");
                        }
                    }
                    break;
                case 3:
                    {
                        if (ltbMarket.SelectedValue != null)
                        {
                            ContentMarketBO obj = new ContentMarketBO();
                            obj.Delete((int)ltbMarket.SelectedValue);
                            FillItemsListbox(ltbTypeOfContentMarket.Text);
                        }
                        else
                        {
                            MessageBox.Show("Bạn phải chọn một hình thức kinh doanh để xóa", "Chú ý!");
                        }
                    }
                    break;
                case 4:
                    {
                        if (ltbPlatform.SelectedValue != null)
                        {
                            ContentPlatformBO obj = new ContentPlatformBO();
                            obj.Delete((int)ltbPlatform.SelectedValue);
                            FillItemsListbox(ltbTypeOfContentPlatform.Text);
                        }
                        else
                        {
                            MessageBox.Show("Bạn phải chọn một nền tảng để xóa", "Chú ý!");
                        }
                    }
                    break;
                default:

                    break;
            }

            InitControl();
        }

        private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
        {
            InitControl();
            int tab = tabControl1.SelectedIndex;
            switch (tab)
            {
                case 0:
                    {

                    }
                    break;
                case 1:
                    {
                        FillItemsListbox(ltbTypeOfContentCategory.Text);
                        FillDetails(Convert.ToInt16(ltbCategory.SelectedValue));
                    }
                    break;
                case 2:
                    {
                        FillItemsListbox(ltbTypeOfContentFormat.Text);
                        FillDetails(Convert.ToInt16(ltbFormat.SelectedValue));
                    }
                    break;
                case 3:
                    {
                        FillItemsListbox(ltbTypeOfContentMarket.Text);
                        FillDetails(Convert.ToInt16(ltbMarket.SelectedValue));
                    }
                    break;
                case 4:
                    {
                        FillItemsListbox(ltbTypeOfContentPlatform.Text);
                        FillDetails(Convert.ToInt16(ltbPlatform.SelectedValue));
                    }
                    break;
                default:

                    break;
            }
        }

        private void ltbTypeOfContent_SelectedIndexChanged(object sender, EventArgs e)
        {
            InitControl();
            TypeOfContentEntity typeOfContentEntity = (new TypeOfContentBO()).SelectOne(Convert.ToInt16(ltbTypeOfContent.SelectedValue));
            if (typeOfContentEntity != null)
            {
                txtTypeOfContentId.Text = typeOfContentEntity.Id.ToString();
                txtTypeOfContentName.Text = typeOfContentEntity.Name;
                txtTypeOfContentDesc.Text = typeOfContentEntity.Description;
            }
        }

        private void ltbTypeOfContentCategory_SelectedIndexChanged(object sender, EventArgs e)
        {
            InitControl();
            FillItemsListbox(ltbTypeOfContentCategory.Text);
            FillDetails(Convert.ToInt16(ltbCategory.SelectedValue));
        }

        private void ltbCategory_SelectedIndexChanged(object sender, EventArgs e)
        {
            FillDetails(Convert.ToInt16(ltbCategory.SelectedValue));
        }

        private void ltbTypeOfContentFormat_SelectedIndexChanged(object sender, EventArgs e)
        {
            InitControl();
            FillItemsListbox(ltbTypeOfContentFormat.Text);
            FillDetails(Convert.ToInt16(ltbFormat.SelectedValue));
        }

        private void ltbFormat_SelectedIndexChanged(object sender, EventArgs e)
        {
            FillDetails(Convert.ToInt16(ltbFormat.SelectedValue));
        }

        private void ltbTypeOfContentMarket_SelectedIndexChanged(object sender, EventArgs e)
        {
            InitControl();
            FillItemsListbox(ltbTypeOfContentMarket.Text);
            FillDetails(Convert.ToInt16(ltbMarket.SelectedValue));
        }

        private void ltbMarket_SelectedIndexChanged(object sender, EventArgs e)
        {
            FillDetails(Convert.ToInt16(ltbMarket.SelectedValue));
        }

        private void ltbTypeOfContentPlatform_SelectedIndexChanged(object sender, EventArgs e)
        {
            InitControl();
            FillItemsListbox(ltbTypeOfContentPlatform.Text);
            FillDetails(Convert.ToInt16(ltbPlatform.SelectedValue));
        }

        private void ltbPlatform_SelectedIndexChanged(object sender, EventArgs e)
        {
            FillDetails(Convert.ToInt16(ltbPlatform.SelectedValue));
        }

        #endregion

        

        
    }
}
